package com.liuzhongni.wahahanews.topicclass;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.adapter.ListViewAdapterOfTopicAnswer;
import com.liuzhongni.wahahanews.circle.CircleImageView;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;

/**
 * Created by dllo on 16/1/25.
 */
public class TopicDetails extends AppCompatActivity implements View.OnClickListener {
    private TopicDetailsFirst detailsFirst;
    private ImageView imageViewDown;
    private ImageView imageViewUp;
    private RelativeLayout relativeLayoutUpAndDown;
    private static int state = 1; //收起状态
    private ListView listView;
    private View view;
    private NetworkImageView imageViewHead;
    private TextView textViewHead;
    private TextView textViewConcernCount;
    private CircleImageView circleImageViewHead;
    private TextView textViewHeadName;
    private TextView textViewTitle;
    private TextView textViewDescription;
    private TextView textViewQuestionCount;
    private TextView textViewAnswerCount;
    private TextView textViewZuixin;
    private TextView textViewZuire;
    private ListViewAdapterOfTopicAnswer adapter;
    private static int type = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.topic_question_answer);


        json();
    }

    private void json() {
        String url1 = "http://c.m.163.com/newstopic/qa/";
        String url2 = ".html";
        String url3 ;
        final Intent intent = getIntent();
        url3 = intent.getStringExtra("expertId");
        Log.d("TopicDetails", url1 + url3 + url2);
        StringRequest request = new StringRequest(url1 + url3 + url2, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("TopicDetails", response);
                Gson gson = new Gson();
                detailsFirst = gson.fromJson(response,TopicDetailsFirst.class);
                Toast.makeText(TopicDetails.this, intent.getStringExtra("position"), Toast.LENGTH_SHORT).show();
                adapter = new ListViewAdapterOfTopicAnswer(detailsFirst.getData(),type,TopicDetails.this);
                init();
                VolleySingleton.getOurInstance().imgJson(imageViewHead,detailsFirst.getData().getExpert().getPicurl());
                VolleySingleton.getOurInstance().imgJson(circleImageViewHead,detailsFirst.getData().getExpert().getHeadpicurl());
                textViewHead.setText(detailsFirst.getData().getExpert().getAlias());
                textViewConcernCount.setText(detailsFirst.getData().getExpert().getConcernCount() + "关注");
                textViewHeadName.setText(detailsFirst.getData().getExpert().getName());
                textViewTitle.setText(detailsFirst.getData().getExpert().getTitle());
                textViewQuestionCount.setText(detailsFirst.getData().getExpert().getQuestionCount()+"提问");
                textViewAnswerCount.setText(detailsFirst.getData().getExpert().getAnswerCount()+"回复");
                listView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("TopicDetails", "失败");
            }
        });
        VolleySingleton.getOurInstance().getQueue().add(request);
    }

    private void init() {
        view = LayoutInflater.from(this).inflate(R.layout.topic_details_item_head,null);
        listView = (ListView) findViewById(R.id.topic_lv_question_answer);
        listView.addHeaderView(view);
        imageViewHead = (NetworkImageView) view.findViewById(R.id.topic_details_item_head_iv);
        circleImageViewHead = (CircleImageView) view.findViewById(R.id.topic_details_item_head_iv_head);
        textViewHead = (TextView) view.findViewById(R.id.topic_details_item_head_tv);
        textViewConcernCount = (TextView) view.findViewById(R.id.topic_details_item_head_tv_concernCount);
        textViewHeadName = (TextView) view.findViewById(R.id.topic_details_item_head_name);
        textViewTitle = (TextView) view.findViewById(R.id.topic_details_item_head_title);
        textViewQuestionCount = (TextView) view.findViewById(R.id.topic_details_item_head_questionCount);

        textViewAnswerCount = (TextView) view.findViewById(R.id.topic_details_item_head_answerCount);
        textViewZuixin = (TextView) view.findViewById(R.id.topic_details_item_head_zuixin);
        textViewZuire = (TextView) view.findViewById(R.id.topic_details_item_head_zuire);
        imageViewDown = (ImageView) view.findViewById(R.id.topic_details_item_head_down);
        imageViewUp = (ImageView) view.findViewById(R.id.topic_details_item_head_up);

        textViewDescription = (TextView) view.findViewById(R.id.topic_details_item_head_description);
        relativeLayoutUpAndDown = (RelativeLayout) view.findViewById(R.id.topic_details_item_head_upanddown);
        relativeLayoutUpAndDown.setOnClickListener(this);
        textViewZuixin.setOnClickListener(this);
        textViewZuire.setOnClickListener(this);
        textViewDescription.setMaxLines(2);
        imageViewUp.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.topic_details_item_head_upanddown:
                if(state==1){
                    textViewDescription.setMaxLines(Integer.MAX_VALUE);
                    textViewDescription.requestLayout();
                    imageViewDown.setVisibility(View.INVISIBLE);
                    imageViewUp.setVisibility(View.VISIBLE);
                    state =2;
                }else if(state == 2){
                    textViewDescription.setMaxLines(2);
                    textViewDescription.requestLayout();
                    imageViewDown.setVisibility(View.VISIBLE);
                    imageViewUp.setVisibility(View.INVISIBLE);
                    state = 1;
                }
                break;
            case R.id.topic_details_item_head_zuixin:
                type = 1;
                textViewZuixin.setBackgroundColor(Color.RED);
                textViewZuixin.setTextColor(Color.WHITE);
                textViewZuire.setBackgroundColor(Color.WHITE);
                textViewZuire.setTextColor(Color.RED);
                adapter.upData();
                break;
            case R.id.topic_details_item_head_zuire:
                type = 2;
                textViewZuixin.setBackgroundColor(Color.WHITE);
                textViewZuixin.setTextColor(Color.RED);
                textViewZuire.setBackgroundColor(Color.RED);
                textViewZuire.setTextColor(Color.WHITE);
                adapter.upData();
                break;
        }
    }
}
